package cn.simple.manager;

import cn.simple.manager.Repository.UserResponsitory;
import cn.simple.manager.todo.User;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import org.thymeleaf.util.ArrayUtils;

import java.io.FileInputStream;
import java.nio.charset.Charset;

@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("test")
public class test {

    @Autowired
    cn.simple.manager.Repository.ceshiResponsitory ceshiResponsitory;

    @Test
    public void testOne(){
       Assert.assertFalse("Fail",ceshiResponsitory==null&&ceshiResponsitory.count()==0);;
    }

    @Autowired
    UserResponsitory userResponsitory;

    @Test
    public void testName(){

        User user = new  User();
        user.setUsername("admin");
        Assert.assertFalse("Fail not check Account",userResponsitory.getByNameOrMobile("admin")==null);
    }

    @Autowired
    private WebApplicationContext wac;

    private MockMvc mockMvc;
    @Before
    public void setup() {
        mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
    }

    @Test
    public void testUpload(){
        try {
            String result =  mockMvc.perform(
                    MockMvcRequestBuilders
                            .multipart("/upload/imageBatch")
                            .file(
                                    new MockMultipartFile("file", "身份证.jpg", ",multipart/form-data", new FileInputStream("C:\\Users\\TK\\Desktop\\身份证.jpg"))
                            )
            ).andExpect(MockMvcResultMatchers.status().isOk())
                    .andReturn().getResponse().getContentAsString(Charset.forName("utf-8"));
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
